/*
 * JDockToolBar Copyright (C) 2010 The JDockToolBar Team
 * This program comes with ABSOLUTELY NO WARRANTY; for details see LICENSE
 * This is free software, and you are welcome to redistribute it
 * under certain conditions; see LICENSE for details.
 */
package com.googlecode.jdocktoolbar.swing.ui;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.RadialGradientPaint;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.geom.Path2D;
import java.awt.image.BufferedImage;

import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.plaf.basic.BasicButtonUI;

import com.googlecode.jdocktoolbar.swing.JDockButton;

// TODO: Auto-generated Javadoc
/**
 * The Class DockButtonUI.
 * 
 * @author Marco Biscaro
 */
public class DockButtonUI extends BasicButtonUI {

	/** The Constant OPACITY. */
	private static final float OPACITY = 0.4f;

	/** The Constant FADE_HEIGHT. */
	private static final float FADE_HEIGHT = 0.3f;

	private static final int OFFSET = 25;

	/**
	 * Creates the UI for {@link JDockButton}.
	 * 
	 * @param c
	 *            the c
	 * @return the dock button ui
	 */
	public static DockButtonUI createUI(JComponent c) {
		return new DockButtonUI();
	}

	@Override
	public Dimension getPreferredSize(JComponent c) {
		JDockButton b = (JDockButton) c;

		ImageIcon icon = (ImageIcon) b.getIcon();
		Graphics g = b.getGraphics();

		int textWidth = g.getFontMetrics().stringWidth(b.getText()) + 20;

		int w = Math.max(icon.getIconWidth(), textWidth);
		int h = (int) (OFFSET + icon.getIconHeight() * (1 + FADE_HEIGHT * 2));

		return new Dimension(w, h);
	}

	@Override
	protected void paintIcon(Graphics g, JComponent c, Rectangle iconRect) {
		JDockButton b = (JDockButton) c;
		Image image = ((ImageIcon) b.getIcon()).getImage();
		Graphics2D g2d = (Graphics2D) g.create();
		int imageWidth = image.getWidth(c);
		int imageHeight = image.getHeight(c);

		// paints the icon
		g2d.drawImage(image, (b.getWidth() - imageWidth) / 2, OFFSET, c);

		// paints the reflection
		g2d.translate((b.getWidth() - imageWidth) / 2, OFFSET + 2 * imageHeight + imageHeight / 50);
		g2d.scale(1, -1);
		BufferedImage reflection = new BufferedImage(imageWidth, imageHeight,
				BufferedImage.TYPE_INT_ARGB);
		Graphics2D rg = reflection.createGraphics();
		rg.drawImage(image, 0, 0, c);
		rg.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_IN));
		rg.setPaint(new GradientPaint(0, imageHeight * FADE_HEIGHT, new Color(
				0.0f, 0.0f, 0.0f, 0.0f), 0, imageHeight, new Color(0.0f, 0.0f,
				0.0f, OPACITY)));
		rg.fillRect(0, 0, imageWidth, imageHeight);
		rg.dispose();
		g2d.drawRenderedImage(reflection, null);

		g2d.dispose();
	}

	@Override
	protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect,
			Rectangle textRect, Rectangle iconRect) {
		// FIXME this is very ugly
		JDockButton b2 = (JDockButton) b;

		Image image = ((ImageIcon) b.getIcon()).getImage();
		Graphics2D g2d = (Graphics2D) g.create();
		int imageWidth = image.getWidth(b2);
		int imageHeight = image.getHeight(b2);

		int w = imageWidth / 3;
		int h = imageHeight / 10;

		Point center = new Point(b.getWidth() / 2, OFFSET + imageHeight + h + h
				/ 2);

		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);

		float[] fractions = { 0, 0.8f, 1 };
		Color[] colors = { Color.WHITE, new Color(220, 200, 255),
				new Color(0, 0, 255) };
		g2d.setPaint(new RadialGradientPaint(center, h, fractions,
				colors));
		// g2d.setColor(Color.RED);
		g2d.fillOval((b.getWidth() - w) / 2, OFFSET + imageHeight + h, w, h);

		g2d.dispose();
	}

	@Override
	protected void paintButtonPressed(Graphics g, AbstractButton b) {
		JDockButton b2 = (JDockButton) b;

		Image image = ((ImageIcon) b.getIcon()).getImage();
		Graphics2D g2d = (Graphics2D) g.create();
		int imageWidth = image.getWidth(b2);
		int imageHeight = image.getHeight(b2);

		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);

		Point center = new Point(imageWidth / 2, OFFSET + imageHeight / 2);
		float[] fractions = { 0, 1 };
		Color[] colors = { new Color(0, 0, 0, 0), new Color(100, 180, 255, 160) };
		g2d.setPaint(new RadialGradientPaint(center, imageWidth / 2, fractions,
				colors));

		g2d.fillOval(1 + (b.getWidth() - imageWidth) / 2, OFFSET + 1,
				imageWidth - 1, imageHeight - 1);

		g2d.dispose();
	}

	@Override
	protected void paintText(Graphics g, AbstractButton b, Rectangle textRect,
			String text) {
		JDockButton b2 = (JDockButton) b;

		if (!b2.isShowText() || !b2.isMouseOver()) {
			return;
		}

		Graphics2D g2d = (Graphics2D) g.create();

		String s = b.getText();

		int h = g2d.getFontMetrics().getHeight();
		int w = g2d.getFontMetrics().stringWidth(s);

		int x = (b.getWidth() - w) / 2;

		int cX = b.getWidth() / 2;

		Path2D p = new Path2D.Float();
		p.moveTo(x, 0);
		p.curveTo(x - 10, 0, x - 10, h, x, h);
		p.lineTo(cX - 5, h);
		p.lineTo(cX, h + 5);
		p.lineTo(cX + 5, h);
		p.lineTo(x + w, h);
		p.curveTo(x + w + 10, h, x + w + 10, 0, x + w, 0);
		p.closePath();

		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);

		g2d.setColor(Color.GRAY);
		g2d.fill(p);

		g2d.setColor(Color.DARK_GRAY);
		g2d.draw(p);

		g2d.setColor(Color.WHITE);
		g2d.drawString(s, x + 1, 3 * h / 4 + 1);

		g2d.dispose();
	}

}
